<?php
/**
 * 管理员
 */

namespace App\Http\Controllers\Admin;

use App\Models\User;
use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $result = $this->_repoService->index($request);
        return $this->display('user.index', $result);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return $this->display('user.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        if ($request->ajax()) {
            $result = $this->_repoService->store($request->all());
            if ($result['code'] == 1) {
                return $this->success('新增成功');
            } else {
                return $this->error($result['message']);
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $admin = User::find($id);
        $this->assign('vo', $admin);
        return $this->display('user.edit');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        if ($request->ajax()) {
            $result = $this->_repoService->update(['id' => $id], $request->all());
            if ($result['code'] == 1) {
                return $this->success('更新成功');
            } else {
                return $this->error($result['message']);
            }
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        if ($request->ajax()) {
            $result = $this->_repoService->destroy(['id' => $id]);
            if ($result['code'] == 1) {
                return $this->success('删除成功');
            } else {
                return $this->error($result['message']);
            }
        }
    }

    /**
     * 管理员状态调整
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function switch_status(Request $request)
    {
        if ($request->ajax()) {
            $id = $request->input('id', 0);
            $value = $request->input('value', 0);
            if ($value == 0) {
                $value = 1;
            } else {
                $value = 0;
            }
            $admin = User::find($id);
            if (is_null($admin)) {
                return $this->error('管理员不存在');
            } else {
                if ($admin->is_sys == 1) {
                    return $this->error('超级管理员账号不能修改');
                }
                $admin->status = $value;
                $admin->save();
                return $this->success('success', ['value' => $value]);
            }
        }
    }

    /**
     * 管理员修改密码
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function password(Request $request)
    {
        $id = $request->input('id', 0);
        $this->assign('id', $id);
        $admin_self = 0;
        if ($id == 0) {
            $id = Auth::id();
            $admin_self = 1;
        }
        $admin = User::find($id);
        if ($request->isMethod('post')) {
            $password = $request->input('password', '');
            $repassword = $request->input('repassword', '');
            if (!preg_match('/^[\S]{6,20}$/', $password, $matches)) {
                return $this->error('密码必须6到12位，且不能出现空格');
            }
            if ($password != $repassword) {
                return $this->error('两次密码输入不一致');
            }

            $admin->password = Hash::make($password);
            if (false !== $admin->save()) {
                $datas = ['redirect' => url('user')];
                if ($admin_self) {
                    $datas['redirect'] = url('login');
                }
                return $this->success('密码修改成功', $datas);
            } else {
                return $this->error('密码修改失败');
            }
        }
        $this->assign('vo', $admin);
        return $this->display('user.password');
    }

    /**
     * 资料修改
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function userinfo(Request $request)
    {
        //
        $admin = Auth::user();
        if ($request->ajax()) {
            $result = $this->_repoService->update(['id' => $admin->id], $request->all());
            if ($result['code'] == 1) {
                return $this->success('更新成功');
            } else {
                return $this->error($result['message']);
            }
        }
        $this->assign('vo', $admin);
        return $this->display('user.userinfo');
    }

    /**
     * 管理员角色设置
     *
     * @param Request $request
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function authorization(Request $request, $id)
    {
        $admin = User::find($id);
        if ($request->ajax() && $request->isMethod('post')) {
            //角色IDs
            $role_ids = $request->input('role_ids', []);
            if (empty($role_ids)) {
                DB::table('role_admin')->where(['admin_id' => $id])->delete();
                return $this->success('设置成功');
            } else {
                DB::table('role_admin')->where(['admin_id' => $id])->whereNotIn('role_id',$role_ids)->delete();
                foreach($role_ids as $role_id){
                    $admin->assignRole(Role::find($role_id));
                }
                return $this->success('设置成功');
            }
        }
        /**
         * 获取所有角色
         */
        $roles = Role::all();
        $this->assign('roles', $roles);
        $this->assign('vo',$admin);
        $this->assign('id', $id);
        return $this->display('user.authorization');
    }
}
